package com.example.demo;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;

public class MainActivity extends AppCompatActivity {
    private Button btn_http;
    private Button btn_imgRequest;
    private Button btnImageLoader;
    private Button btnNetworkImageView;
    private ImageView imageView;
    private RequestQueue requestQueue;
    private Handler handler;

    private MyImageCache myImageCache;

    private NetworkImageView networkImageView;
    private String url = "http://img02.gongjiao.com/2016-09/14/57d3b636/57d3b6360a54c9dc182727d5/1473859850020_379933.jpg";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        requestQueue = Volley.newRequestQueue(MainActivity.this);
        myImageCache = new MyImageCache();

        //控件
        MyfindViewById();

        btn_http.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        useHttpUrlConnection();
                    }
                }).start();
            }
        });

        btn_imgRequest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                useImageRequest();
            }
        });

        btnImageLoader.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                useImageLoader();
            }
        });
        
        btnNetworkImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                useNetworkImageView();
            }
        });
    }

    //useHttpUrlConnection
    private void useHttpUrlConnection() {
        String strurl = "http://img02.gongjiao.com/2016-09/14/57d3b636/57d3b6360a54c9dc182727d5/1473859850020_379933.jpg";
        try {
            URL url = new URL(strurl);
            //打开链接
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            //获取流
            InputStream inputStream = httpURLConnection.getInputStream();
            //图片格式
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

            //需要线程
            //发送图片
            Message message = new Message();
            message.what = 1001;
            message.obj = bitmap;
            handler.sendMessage(message);

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //ImageRequest
    private void useImageRequest() {
        // String url = "http://img02.gongjiao.com/2016-09/14/57d3b636/57d3b6360a54c9dc182727d5/1473859850020_379933.jpg";
        ImageRequest imageRequest = new ImageRequest(url, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap bitmap) {
                imageView.setImageBitmap(bitmap);
            }
        }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                imageView.setImageResource(R.drawable.error);
            }
        });
        requestQueue.add(imageRequest);
    }

    //useImageLoader
    private void useImageLoader() {
        //myImageCache  内存缓存
        ImageLoader imageLoader = new ImageLoader(requestQueue, myImageCache);

        ImageLoader.ImageListener listener = ImageLoader.getImageListener(imageView,R.drawable.load,R.drawable.error);
        imageLoader.get(url,listener,0,0);
    }

    //NetworkImageView
    private void useNetworkImageView() {
        ImageLoader imageLoader = new ImageLoader(requestQueue, myImageCache );

        networkImageView.setDefaultImageResId(R.drawable.load);
        networkImageView.setErrorImageResId(R.drawable.error);
        networkImageView.setImageUrl(url,imageLoader);


    }

    //控件
    private void MyfindViewById() {
        btn_http = findViewById(R.id.btn_http);
        btn_imgRequest = findViewById(R.id.btn1);
        imageView = findViewById(R.id.image_view);
        btnImageLoader = findViewById(R.id.btn2);
        btnNetworkImageView = findViewById(R.id.btn3);
        networkImageView = findViewById(R.id.networkimageview);

        //httpUrlConnection
        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if (msg.what ==1001){
                    imageView.setImageBitmap((Bitmap) msg.obj);
                    return true;
                }
                return false;
            }
        });

    }
}
